//
//  StringTool.m
//  mengbutou
//
//  Created by Jiafei Wei on 16/7/6.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "StringTool.h"
#import <Foundation/Foundation.h>
#define DEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd'T'HH:mm:ss'Z'")
#define DEFAULT_DATE_FORMAT (@"yyyy-MM-dd")
#define DEFAULT_TIME_FORMAT (@"HH:mm:ss'Z'")
@implementation StringTool
+(BOOL) isEmpty:(NSString*)text
{
    if (text == nil) {
        return YES;
    }
    if ([text length] == 0 ) {
        return YES;
    }
    return NO;
}
+(BOOL) boolValue:(NSDictionary*)dict forKey:(id)key
{
    id bl = [dict objectForKey:key];
    if ( bl == nil || [bl isKindOfClass:[NSNull class]]) {
        return NO;
    }
    if ( [bl isKindOfClass:[NSString class]]) {
        NSString* cstr = [bl lowercaseString];
        if ( [cstr isEqualToString:@"yes"] || [cstr isEqualToString:@"true"]) {
            return YES;
        }
        return [cstr boolValue];
    }
    if ( CFBooleanGetValue((CFBooleanRef)bl) ) {
        return YES;
    }
    return NO;
}

+(float) floatValue:(NSDictionary*)dict forKey:(id)key{
    id bl = [dict objectForKey:key];
    if ( bl == nil || [bl isKindOfClass:[NSNull class]]) {
        return 0.0f;
    }
    return [bl floatValue];
}

+(NSInteger) intValue:(NSDictionary*)dict forKey:(id)key
{
    id bl = [dict objectForKey:key];
    if ( bl == nil || [bl isKindOfClass:[NSNull class]]) {
        return 0.0f;
    }
    return [bl floatValue];
}

+(NSArray*) arrayValue:(NSDictionary*)dict forKey:(id)key
{
    id bl = [dict objectForKey:key];
    if ( bl == nil || [bl isKindOfClass:[NSNull class]]) {
        return nil;
    }
    if ( [bl isKindOfClass:[NSArray class]]) {
        return (NSArray*)bl;
    }
    return nil;
}

+(NSDictionary*) dictionaryValue:(NSDictionary*)dict forKey:(id)key
{
    id bl = [dict objectForKey:key];
    if ( bl == nil || [bl isKindOfClass:[NSNull class]]) {
        return nil;
    }
    return (NSDictionary*)bl;
}

+(id) noneNullValue:(NSDictionary*)dict forKey:(id)key
{
    id bl = [dict objectForKey:key];
    if ( bl == nil || [bl isKindOfClass:[NSNull class]]) {
        return nil;
    }
    return bl;
}

+(BOOL) stringEquals:(NSString*)str1 to:(NSString*)str2
{
    if ( str1 == nil || str2 == nil ) {
        return NO;
    }
    return [str1 compare:str2 options:NSCaseInsensitiveSearch] == NSOrderedSame;
}

+(BOOL) caseEquals:(NSString*)str1 to:(NSString*)str2
{
    return (str1 == nil || str2 == nil) ? NO : [str1 isEqualToString:str2];
}

+(BOOL)startWith:(NSString*)prefix forString:(NSString*)text;
{
    if ( text != nil && prefix != nil ){
        if ( [prefix length] > [text length] ) {
            return NO;
        }
        
        NSString* prestr = [text substringToIndex:[prefix length]];
        if ( [self stringEquals:prestr to:prefix] ) {
            return YES;
        }
        
    }
    return NO;
}

+(BOOL)endWith:(NSString*)suffix forString:(NSString*)text
{
    if ( text != nil && suffix != nil ){
        if ( [suffix length] > [text length] ) {
            return NO;
        }
        NSInteger len = [text length] - [suffix length];
        NSString* sufstr = [text substringFromIndex:len];
        if ( [self caseEquals:sufstr to:suffix] ) {
            return YES;
        }
        
    }
    return NO;
}

+(BOOL) isURLString:(NSString*)text
{
    if ( [text length] > 6 ) {
        NSString* prefix = [text substringToIndex:6];
        if ( [self stringEquals:prefix to:@"http:/"] || [self stringEquals:prefix to:@"https:"] ) {
            return YES;
        } else if ( [self stringEquals:prefix to:@"local:" ] ){
            return YES;
        }
    }
    if ( [self startWith:@"/" forString:text] ){
        return YES;
    }
    return NO;
}

+(NSString*) refineUrl:(NSString*)url
{
    return url;
}

+(BOOL)booleanToBool:(id)bobj
{
    if ( bobj == nil ) {
        return NO;
    }
    if ( [bobj isKindOfClass:[NSString class]] ) {
        return [bobj caseInsensitiveCompare:@"yes"] == 0 || [bobj caseInsensitiveCompare:@"true"] == 0;
    }else if ( [bobj respondsToSelector:@selector(boolValue)] ){
        return [bobj boolValue];
    }else {
        return CFBooleanGetValue((CFBooleanRef)bobj);
    }
    
    return NO;
}

+(NSDate *)stringToDate:(NSString *)string withFormat:(NSString*)fmt{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSString* format = fmt == nil ? DEFAULT_DATE_TIME_FORMAT : fmt;
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:string];
    return date;
}

+(NSString*)dateToString:(NSDate*)date withFormat:(NSString*)fmt
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSString* format = fmt == nil ? DEFAULT_DATE_FORMAT : fmt;
    [formatter setDateFormat:format];
    NSString* dateStr = [formatter stringFromDate:date];
    return dateStr;
}
+(NSString *)getUUIDString{
    CFUUIDRef uuidObj =CFUUIDCreate(nil);
    NSString *uuidString =(__bridge_transfer NSString*)CFUUIDCreateString(nil,uuidObj);
    CFRelease(uuidObj);
    return uuidString;
}

+(NSString *) stringDeleteString:(NSString *)str
{
    NSMutableString *str1 = [NSMutableString stringWithString:str];
    for (int i = 0; i < str1.length; i++) {
        unichar c = [str1 characterAtIndex:i];
        NSRange range = NSMakeRange(i, 1);
        if (c == '-') { //此处可以是任何字符
            [str1 deleteCharactersInRange:range];
            --i;
        }
    }
    NSString *newstr = [NSString stringWithString:str1];
    return newstr;
}
@end
